/*
 * @(#)OpenRecentAction.java  1.0  June 15, 2006
 *
 * Copyright (c) 1996-2006 by the original authors of JHotDraw
 * and all its contributors ("JHotDraw.org")
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JHotDraw.org ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with
 * JHotDraw.org.
 */

package umage.core;

import org.jhotdraw.xml.DefaultDOMFactory;
import org.jhotdraw.xml.NanoXMLLiteDOMInput;

import java.awt.event.*;

import java.io.*;

import org.jhotdraw.app.Application;
import org.jhotdraw.app.Project;
import org.jhotdraw.app.action.OpenRecentAction;
/**
 * OpenRecentAction.
 *
 * @author Werner Randelshofer.
 * @version 1.0 June 15, 2006 Created.
 */
public class MultiOpenRecentAction extends OpenRecentAction {
	protected File file;
	
    /** Creates a new instance. */
    public MultiOpenRecentAction(Application app, File file) {
        super(app, file);
        this.file = file;
    }
    
    public void actionPerformed(ActionEvent evt) {
        final Application app = getApplication();
        if (app.isEnabled()) {
            app.setEnabled(false);
            if (!determineSelectedProject(file)) {
            	app.setEnabled(true);
            	return;
            }
            Project p = app.createProject();
            app.add(p);
            app.show(p);
            openFile(p);
        }
    }
    
    protected boolean determineSelectedProject(File file) {
    	InputStream in = null;
    	try {
    		if (file == null) return false;
    		in = new BufferedInputStream(new FileInputStream(file));
    		NanoXMLLiteDOMInput domi = new NanoXMLLiteDOMInput(new DefaultDOMFactory(), in);
    		domi.openElement(0);
    		String projectClass = domi.getTagName();
    		domi.closeElement();
    		UMageApplicationModel model = (UMageApplicationModel) getApplication().getModel();
    		model.setProjectClass(Class.forName("umage.projects." + projectClass));
    		return true;
    	} catch (Exception ex) {
    		ex.printStackTrace();
    		return false;
    	} finally {
    		if (in != null) try { in.close(); } catch (IOException e) {};
    	}
    }

}
